package com.acc.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.acc.entity.AdminRole;
import com.acc.entity.Msg;
import com.acc.service.AuthorityService;
import com.acc.service.impl.AuthorityServiceImpl;
import com.alibaba.fastjson.JSON;
/**
 * 
 * @author 朱春阳
 *
 */
public class AddServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	/**
	 * 往ADMIN-ROLE关系表中添加一条记录
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		AuthorityService authorityService = new AuthorityServiceImpl();
		
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		// 从输入框中取出值
		String adminName = req.getParameter("adminName");
		String adminRole = req.getParameter("adminRole");

		
		// 将取出的值封装起来
		AdminRole adminrole = new AdminRole();
		adminrole.setAdminName(adminName);
		adminrole.setAdminRole(adminRole);

		// 增加记录
		boolean flag = authorityService.addAdminRole(adminrole);

		// 存储判断的信息
		Msg msg = new Msg();
		String result = null;
		if (flag) {
			msg.setErrorMsg(null);
			msg.setSuccess(true);
			result = JSON.toJSONString(msg);
		} else {
			msg.setErrorMsg("增加失败");
			msg.setSuccess(false);
			result = JSON.toJSONString(msg);
		}
		
		resp.getWriter().append(result);

	}

}
